package com.yungu.swift.api.route.v1.passenger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.api.facade.PassengerFacade;
import com.yungu.swift.assets.driver.model.dto.CarModelDto;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.order.model.param.ApiOrderUuidParam;
import com.yungu.swift.order.model.param.ApiValuationParam;
import com.yungu.swift.order.model.vo.OrderEstimateVo;
import com.yungu.swift.order.service.OrderFareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Zerrion
 */

@Controller
@RequestMapping(value = "/api/v1/passenger")
@Api(value = "【乘客】【订单】【专车】乘客专车订单接口", tags = "乘客专车订单相关操作")
public class SpecialOrderApi extends BaseApi {

    @Autowired
    private PassengerFacade passengerFacade;
    @Reference
    private OrderFareService orderFareService;


    @RequestMapping(value = {"/token/specialOrder/getRealtimeFare", "/token/express/order/getRealTimeFare"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "获取订单的时时价格和时间", notes = "获取订单的时时价格和时间")
    public ResponseData<OrderEstimateVo> getRealtimeFare(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid,
            HttpServletRequest request) {
        ApiOrderUuidParam orderUuidParam = new ApiOrderUuidParam();
        orderUuidParam.setOrderUuid(orderUuid);
        orderUuidParam.setUserUuid(getUserUid(request));
        return orderFareService.getRealtimeFare(orderUuidParam);
    }


    @RequestMapping(value = {"/specialOrder/valuation"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "估价接口", notes = "valuation", response = ResponseData.class)
    public ResponseData<Map<String, Object>> valuation(
            @ApiParam(name = "token", value = "token") @RequestParam(required = false) String token,
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "planTrip", value = "预估里程(公里)", required = true) @RequestParam Double planTrip,
            @ApiParam(name = "planTime", value = "预估时长(分钟)", required = true) @RequestParam Integer planTime,
            @ApiParam(name = "departTime", value = "出发时间") @RequestParam(required = false) Long departTime,
            @ApiParam(name = "carModelUuid", value = "车型id", required = true) @RequestParam String carModelUuid,
            @ApiParam(name = "adCode", value = "区域编码", required = true) @RequestParam String adCode,
            @ApiParam(name = "serviceFare", value = "附加服务费用") @RequestParam(required = false) Double serviceFare,
            @ApiParam(value = "订单类型（送机8，接机9 预约单2可传可不传）", name = "orderType") @RequestParam(required = false) Integer orderType,
            @ApiParam(value = "起点经度", name = "originLng", required = true) @RequestParam(required = false) Double originLng,
            @ApiParam(value = "起点纬度", name = "originLat", required = true) @RequestParam(required = false) Double originLat,
            @ApiParam(value = "终点经度", name = "destLng", required = true) @RequestParam(required = false) Double destLng,
            @ApiParam(value = "终点纬度", name = "destLat", required = true) @RequestParam(required = false) Double destLat,
            @ApiParam(value = "设备唯一标识符", name = "diu", required = true) @RequestParam(required = false) String diu,
            HttpServletRequest request) {
        ApiValuationParam valuationParam = new ApiValuationParam();
        valuationParam.setAppid(appid);
        valuationParam.setUserUuid(getUserUid(request));
        valuationParam.setBusinessType(CommonConstant.BUSINESS_TYPE_SPEC);
        Integer typeTrip = 1;
        if (orderType != null && orderType.compareTo(8) == 0) {
            typeTrip = 4;
        }
        if (orderType != null && orderType.compareTo(9) == 0) {
            typeTrip = 5;
        }
        valuationParam.setTypeTrip(typeTrip);
        valuationParam.setAdCode(adCode);
        valuationParam.setCarModelUuid(carModelUuid);
        valuationParam.setDepartTime(new Date(departTime));
        valuationParam.setPlanTime(planTime);
        valuationParam.setPlanTrip(planTrip);
        return orderFareService.commonValuation(valuationParam);
    }


    @RequestMapping(value = {"/carModelsLevel/list"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "获取车型列表接口", notes = "车型接口", response = ResponseData.class)
    public ResponseData<List<CarModelDto>> getList(
            @ApiParam(name = "token", value = "token") @RequestParam(required = false) String token,
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid) {
        return passengerFacade.listCarModel(null, appid, CommonConstant.BUSINESS_TYPE_SPEC);
    }


    @RequestMapping(value = {"/specialOrder/listTypeTime"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "获取日租半日租条件数据源接口", notes = "获取日租半日租条件数据源接口", response = ResponseData.class)
    public ResponseData<List<Map<String, Object>>> getList(
            @ApiParam(name = "carModelUuid", value = "车型UUID", required = true) @RequestParam String carModelUuid,
            @ApiParam(name = "adCode", value = "区域编码", required = true) @RequestParam String adCode,
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid) {
        return passengerFacade.listTypeTime(adCode, carModelUuid, appid, null);
    }

    @RequestMapping(value = {"/specialOrder/valuateRental"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "日租半日租估价接口", notes = "日租半日租估价接口", response = ResponseData.class)
    public ResponseData<Map<String, Object>> valuation(
            @ApiParam(name = "token", value = "token") @RequestParam(required = false) String token,
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "fareRuleUuid", value = "计价规则UUID", required = true) @RequestParam String fareRuleUuid) {
        return passengerFacade.valuateRent(token, appid, fareRuleUuid);
    }

}
